package com.example.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.entity.AccDept;
import com.example.framwork.code.CodeConst;
import com.example.framwork.result.ProjectResult;
import com.example.service.AccDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RestController
@Slf4j
@RequestMapping("/accdept")
public class AccDeptController {

    @Autowired
   private AccDeptService accDeptService;

  //  public AccDeptController(AccDeptService accDeptService) {
//        this.accDeptService = accDeptService;
//    }

  //  private final AccountService accountService;
    @RequestMapping(value = "/getAllDept", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult getDept() {
       List<AccDept> accDeptList=accDeptService.getAll();

        // PageInfo<Map<String, Object>> pageInfo = accDeptService.getAll();
        //return new ProjectResult(CodeConst.SUCCESS, "查询成功", pageInfo.getList());
        return new ProjectResult(CodeConst.SUCCESS, "查询成功",accDeptList);
       // return null;
    }
    @RequestMapping(value = "/getDeptList", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult getDeptList() {
        //多ids查询
        List<AccDept> accDepts=accDeptService.selectBatchIds(Arrays.asList(1,2,10,11));
        for (AccDept accTemp:accDepts) {
            System.out.println(accTemp);
        }
        //单个is查询
        AccDept accDeptOne=accDeptService.selectById(1L);
        System.out.println(accDeptOne);

        //Condition的查询
        Page<AccDept> accDeptSelectPage=accDeptService.selectPage(new Page<>(2,19),new EntityWrapper<AccDept>().eq("kind",1).like("dept_name","院"));
        Map<String,Object> objectMap=accDeptSelectPage.getCondition();
        for(AccDept accDept: accDeptSelectPage.getRecords()){
            System.out.println(accDept);
        }

        //Wrapper查询
        List<AccDept> accDeptSelectList=accDeptService.selectList(new EntityWrapper<AccDept>().eq("kind",1).like("dept_name","院"));
        for(AccDept accDept:accDeptSelectList){
            System.out.println(accDept);
        }
        return new ProjectResult(CodeConst.SUCCESS, "查询成功",accDeptSelectPage);
        //return null;
    }
    //页面内分页
    @RequestMapping(value = "/getDeptListPage", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult getDeptListPage(@RequestParam Integer pages, @RequestParam Integer rows) {
        Map<String, Object> mapQuery = new HashMap<>();
        mapQuery.put("kind", "1");

        PageHelper.startPage(pages, rows);
        EntityWrapper<AccDept> ew = new EntityWrapper<>();
       //45                                                       ew.eq("kind", 1).like("dept_name", "院");
        ew.orderBy("deptId");
        List<AccDept> accDeptList = accDeptService.selectList(ew);
        PageInfo pageInfo = new PageInfo<>(accDeptList);

        return new ProjectResult(CodeConst.SUCCESS, "查询成功", pageInfo);

    }
    //切面分页
    @RequestMapping(value = "/getDeptListPagePoint", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult getDeptListPagePoint(@RequestParam Map<String, Object> params) {
        //  log.trace("this is trace");
        // log.info("this is info");

        //params.put("deptName", "其他");
       // params.put("deptName",params.)
        PageInfo<AccDept> accDeptSelectList = accDeptService.listAccDept(params);
        Object keyValue = params.get("key");
        //log.info(keyValue.toString());
        ProjectResult projectResult = new ProjectResult(CodeConst.SUCCESS, "查询成功", accDeptSelectList.getList(), accDeptSelectList.getTotal());
        return projectResult;

//        if (keyValue != null && Integer.parseInt(keyValue.toString()) % 2 == 0) {
//            return "this is null";
//        } else {
//            return accDeptSelectList.toString();
//        }
//        //return null;

    }

    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult addDept() {
        AccDept accDept=new AccDept();
        accDept.setUuid(UUID.randomUUID().toString());
        accDept.setDeptName("deptName:");
        accDept.setGmtCreate(new Date());
        accDept.setGmtModified(new Date());

        boolean bRes= accDeptService.saveOne(accDept);
        return new ProjectResult(CodeConst.SUCCESS, "查询成功",bRes);
    }
    @RequestMapping(value = "/addList", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult addDeptList() {
        List<AccDept> accDeptList = new ArrayList<>();
        for (Integer i = 1; i < 2; i++) {
            AccDept tempDept = new AccDept();
            tempDept.setUuid(UUID.randomUUID().toString());

            tempDept.setDeptName("deptName:" + i.toString());
            tempDept.setGmtCreate(new Date());
            tempDept.setGmtModified(new Date());
            accDeptList.add(tempDept);
        }
        boolean bRes= accDeptService.saveBatch(accDeptList);
        return new ProjectResult(CodeConst.SUCCESS, "查询成功",bRes);
    }
    @RequestMapping(value = "/testError", method = {RequestMethod.GET, RequestMethod.POST})
    public ProjectResult testError() {
        try {
            Integer nRes = 9 / 0;
        }
        catch (Exception e)
        {
            throw new NumberFormatException("numberexception"+e.getMessage());
           // return new ProjectResult(CodeConst.INTERNAL_ERROR, e.getMessage());
        }
        return new ProjectResult(CodeConst.SUCCESS, "查询成功");

    }

    /*
    @GetMapping
    public ProjectResult get(@RequestParam String key, @RequestParam Integer num) {
        if (StringUtils.isEmpty(num) || num == 0) {
            num = 10;
        }
        List<Account> listacc = accountService.selectList(new EntityWrapper<Account>()
                .like(!StringUtils.isEmpty(key), "logon_name", key)
                .orderBy(!StringUtils.isEmpty(key), "length(logon_name)").last("limit " + num)
        );
        List<Accountres> listres = new ArrayList<>();
        if (!CollectionUtils.isEmpty(listacc)) {
            for (Account acc : listacc) {
                Accountres res = new Accountres();
                res.setAccNo(acc.getAccNo());
                res.setLogonName(acc.getLogonName());
                res.setTrueName(acc.getTrueName());
                listres.add(res);
            }
        }
        return new ProjectResult(CodeConst.SUCCESS, "com_search_success", listres);
    }
     */
}
